package com.ruoyi.web.controller.fantasy;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.huanzhidadi.fantasy.bean.domain.FantasyShop;
import com.huanzhidadi.fantasy.bean.domain.FantasyShopItem;
import com.ruoyi.fantasy.domain.FantasyItemType;
import com.ruoyi.fantasy.domain.FantasyShopBO;
import com.ruoyi.fantasy.domain.FantasyShopItemBO;
import com.ruoyi.fantasy.service.IFantasyItemtypeService;
import com.ruoyi.fantasy.service.IFantasyShopItemService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fantasy.service.IFantasyShopService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 商店 信息操作处理
 * 
 * @author wanghui
 * @date 2020-01-29
 */
@Controller
@RequestMapping("/fantasy/fantasyShop")
public class FantasyShopController extends BaseController
{
    private String prefix = "fantasy/fantasyShop";
	
	@Autowired
	private IFantasyShopService fantasyShopService;

	@Autowired
	private IFantasyShopItemService fantasyShopItemService;

	@Autowired
	private IFantasyItemtypeService fantasyItemtypeService;

	@RequiresPermissions("fantasy:fantasyShop:view")
	@GetMapping()
	public String fantasyShop()
	{
	    return prefix + "/fantasyShop";
	}
	
	/**
	 * 查询商店列表
	 */
	@RequiresPermissions("fantasy:fantasyShop:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(FantasyShop fantasyShop)
	{
		startPage();
        List<FantasyShop> list = fantasyShopService.selectFantasyShopList(fantasyShop);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出商店列表
	 */
	@RequiresPermissions("fantasy:fantasyShop:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FantasyShop fantasyShop)
    {
    	List<FantasyShop> list = fantasyShopService.selectFantasyShopList(fantasyShop);
        ExcelUtil<FantasyShop> util = new ExcelUtil<FantasyShop>(FantasyShop.class);
        return util.exportExcel(list, "fantasyShop");
    }
	
	/**
	 * 新增商店
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存商店
	 */
	@RequiresPermissions("fantasy:fantasyShop:add")
	@Log(title = "商店", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(FantasyShop fantasyShop)
	{
		// 取身份信息
		SysUser user = ShiroUtils.getSysUser();
		fantasyShop.setCreateBy(user.getUserId().toString());
		fantasyShop.setCreateTime(new Date());
		fantasyShop.setUpdateBy(user.getUserId().toString());
		fantasyShop.setUpdateTime(new Date());
		return toAjax(fantasyShopService.insertFantasyShop(fantasyShop));
	}

	/**
	 * 修改商店
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		FantasyShop fantasyShop = fantasyShopService.selectFantasyShopById(id);
		mmap.put("fantasyShop", fantasyShop);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存商店
	 */
	@RequiresPermissions("fantasy:fantasyShop:edit")
	@Log(title = "商店", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(FantasyShop fantasyShop)
	{
		// 取身份信息
		SysUser user = ShiroUtils.getSysUser();
		fantasyShop.setUpdateBy(user.getUserId().toString());
		fantasyShop.setUpdateTime(new Date());
		return toAjax(fantasyShopService.updateFantasyShop(fantasyShop));
	}
	
	/**
	 * 删除商店
	 */
	@RequiresPermissions("fantasy:fantasyShop:remove")
	@Log(title = "商店", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(fantasyShopService.deleteFantasyShopByIds(ids));
	}





	/**
	 * 商店管理-分配道具
	 */
	@GetMapping("/allocItem/{id}")
	public String allocItem(@PathVariable("id") String id, ModelMap mmap)
	{
		FantasyShop fantasyShop = fantasyShopService.selectFantasyShopById(Long.valueOf(id));
		mmap.put("fantasyShop", fantasyShop);
		return prefix + "/allocItem";
	}

	/**
	 * 查询已分配的道具列表
	 */
	@RequiresPermissions("fantasy:fantasyShopItem:list")
	@PostMapping("/allocItem/allocatedItemList")
	@ResponseBody
	public TableDataInfo allocatedItemList(FantasyShopItemBO fantasyShopItemBO)
	{
		startPage();
		List<FantasyShopItemBO> fantasyShopItemBOList = fantasyShopItemService.selectAllocatedList(fantasyShopItemBO);
		//fantasyShopItemService.transforData(fantasyShopItemBOList);
		return getDataTable(fantasyShopItemBOList);
	}

	/**
	 * 取消分配
	 */
	@Log(title = "道具分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocItem/cancel")
	@ResponseBody
	public AjaxResult cancelAllocItem(FantasyShopItemBO fantasyShopItemBO)
	{
		return toAjax(fantasyShopItemService.deleteFantasyShopItem(fantasyShopItemBO));
	}

	/**
	 * 批量取消分配
	 */
	@Log(title = "道具分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocItem/cancelAll")
	@ResponseBody
	public AjaxResult cancelAllocItemAll(String shopId, String ids)
	{
		return toAjax(fantasyShopItemService.deleteAllocItems(shopId, ids));
	}

	/**
	 * 选择道具页面
	 */
	@GetMapping("/allocItem/selectItem/{shopId}")
	public String selectShop(@PathVariable("shopId") String shopId, ModelMap mmap)
	{
		FantasyShop fantasyShop = fantasyShopService.selectFantasyShopById(Long.valueOf(shopId));
		mmap.put("fantasyShop", fantasyShop);
		return prefix + "/selectItem";
	}

	/**
	 * 查询未分配道具列表
	 */
	@RequiresPermissions("fantasy:fantasyShopItem:list")
	@PostMapping("/allocItem/unallocatedList")
	@ResponseBody
	public TableDataInfo unallocatedList(FantasyShopBO fantasyShopBO)
	{
		// 查询指定商店中的道具列表和商店类型
		FantasyShop fantasyShop = fantasyShopService.selectFantasyShopById(Long.valueOf(fantasyShopBO.getId()));
		Integer shopType = fantasyShop.getShopType();
		FantasyShopItem fantasyShopItem = new FantasyShopItem();
		fantasyShopItem.setShopId(fantasyShopBO.getId());
		List<FantasyShopItem> fantasyShopItemList = fantasyShopItemService.selectFantasyShopItemList(fantasyShopItem);
		List<String> shopItemIds = new ArrayList<>();
		for (FantasyShopItem fantasyShopItem1 : fantasyShopItemList) {
			shopItemIds.add(fantasyShopItem1.getItemId());
		}
		// 根据商店类型，排除商店已有道具，查询道具类型列表
		startPage();
		FantasyItemType fantasyItemtype = new FantasyItemType();
		fantasyItemtype.setItemType(String.valueOf(shopType));
		fantasyItemtype.setAllocatedItemList(shopItemIds);
		List<FantasyItemType> fantasyItemTypeList = fantasyItemtypeService.selectFantasyItemtypeListRemoveAllocated(fantasyItemtype);
		fantasyItemtypeService.transforData(fantasyItemTypeList);
		return getDataTable(fantasyItemTypeList);
	}

	/**
	 * 批量选择道具
	 */
	@RequiresPermissions("fantasy:fantasyShopItem:add")
	@Log(title = "道具分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocItem/selectAll")
	@ResponseBody
	public AjaxResult selectAllocShopAll(String shopId, String itemIds)
	{
		return toAjax(fantasyShopItemService.insertAllocItems(shopId, itemIds));
	}

	/**
	 * 修改商店道具
	 */
	@GetMapping("/allocItem/edit/{id}")
	public String editShopItem(@PathVariable("id") Long id, ModelMap mmap)
	{
		FantasyShopItemBO fantasyShopItemBO = fantasyShopItemService.selectFantasyShopItemBOById(id);
		mmap.put("fantasyShopItemBO", fantasyShopItemBO);
		return prefix + "/editShopItem";
	}

	/**
	 * 修改保存商店道具
	 */
	@RequiresPermissions("fantasy:fantasyShopItem:edit")
	@Log(title = "道具", businessType = BusinessType.UPDATE)
	@PostMapping("/allocItem/edit")
	@ResponseBody
	public AjaxResult editSaveShopItem(FantasyShopItem fantasyShopItem)
	{
		return toAjax(fantasyShopItemService.updateFantasyShopItem(fantasyShopItem));
	}

}
